package com.kma.summer2012.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.kma.summer2012.client.ExceptionHandling.CrumbsRPCServiceException;
import com.kma.summer2012.client.services.CrumbsCatalogOfTools.CrumbsCatalogOfToolsService;
import com.kma.summer2012.dao.CrumbsCatalogOfToolsDAO;
import com.kma.summer2012.factory.DAOFactory;
import com.kma.summer2012.factory.SpringBeansFactory;
import com.kma.summer2012.model.CrumbsCatalogOfTools;
import com.kma.summer2012.server.proxy.Proxy;
import com.kma.summer2012.shared.model.UICrumbsToolItem;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CrumbsCatalogOfToolsServiceImpl extends RemoteServiceServlet implements CrumbsCatalogOfToolsService {
    @Override
    public List<UICrumbsToolItem> getAllTools() throws CrumbsRPCServiceException {
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        try {
            List<UICrumbsToolItem> uiTools = new ArrayList<UICrumbsToolItem>();
            List<CrumbsCatalogOfTools> tools;
            tools = (List<CrumbsCatalogOfTools>) factory.getCrumbsCatalogOfToolsDAO().getAll();
            for (CrumbsCatalogOfTools tool : tools) {
                if (!tool.isDelete()) {
                    uiTools.add(Proxy.getInstance().createFromCatalogOfTools(tool));
                }
            }
            return uiTools;
        } catch (Exception e) {
            throw new CrumbsRPCServiceException("Getting list of tools from DataBase failed.", e);
        }
    }

    @Override
    public void saveTool(UICrumbsToolItem uiTool) throws CrumbsRPCServiceException {
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        try {

            CrumbsCatalogOfTools tool = Proxy.getInstance().createCatalogOfTools(uiTool);
            if (uiTool.getId() != 0) {
                factory.getCrumbsCatalogOfToolsDAO().update(tool);
            } else {
                factory.getCrumbsCatalogOfToolsDAO().add(tool);

            }
        } catch (Exception e) {
            throw new CrumbsRPCServiceException("Saving work to DataBase failed.",e);
        }
    }


    @Override
    public void deleteAllTool(List<UICrumbsToolItem> selectedTools) throws CrumbsRPCServiceException {
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        CrumbsCatalogOfToolsDAO dao = (CrumbsCatalogOfToolsDAO) factory.getCrumbsCatalogOfToolsDAO();
        try {
            List<CrumbsCatalogOfTools> tools = new ArrayList<CrumbsCatalogOfTools>();
            for (UICrumbsToolItem uiTool : selectedTools) {
                CrumbsCatalogOfTools tool = Proxy.getInstance().createCatalogOfTools(uiTool);
                tools.add(tool);
            }
            dao.deleteAll(tools);
        } catch (Exception e) {
            throw new CrumbsRPCServiceException("Deletion of tools from DataBase failed.", e);
        }
    }

    @Override
    public UICrumbsToolItem getByNameTool(String name) throws CrumbsRPCServiceException {
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        CrumbsCatalogOfToolsDAO dao = (CrumbsCatalogOfToolsDAO) factory.getCrumbsCatalogOfToolsDAO();
        try {
            UICrumbsToolItem uitool;
            CrumbsCatalogOfTools tool = dao.getByName(name);
            if (tool != null) {
                uitool = Proxy.getInstance().createFromCatalogOfTools(tool);
            } else uitool = null;
            return uitool;
        } catch (Exception e) {
            throw new CrumbsRPCServiceException("Getting the tool from DataBase failed.", e);
        }
    }

}
